
package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.informationGet.LoginLogVO;
import com.example.visitedsys.informationGet.PageVO;
import com.example.visitedsys.pojo.loginLog;
import com.example.visitedsys.service.loginLogService;
import com.wuwenze.poi.ExcelKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 〈登录日志〉<br>
 * 〈〉
 */

/**
 * @author  SongPeng
 * @date  2021/11/12 13:46
 * @version 1.0
 */


@RestController
@RequestMapping("/loginLog")

public class LoginLogController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","登录日志模块");
    }
    private static final Logger logger = LogManager.getLogger(LoginLogController.class);
    @Autowired
    private loginLogService loginLogService;
    /**
     * 日志列表
     *
     * @return
     */
    @RequiresPermissions("loginLog:select")
    @GetMapping("/findLoginLogList")
    public ResponseBean findLoginLogList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize") Integer pageSize,
                                         LoginLogVO loginLogVO) {
        PageVO<LoginLogVO> loginLogList = loginLogService.findLoginLogList(pageNum, pageSize, loginLogVO);
        return ResponseBean.success(loginLogList);
    }
    /**
     * 日志删除
     *
     * */
    @RequiresPermissions("loginLog:delete")
    @DeleteMapping("/delete/{loginId}")
    public ResponseBean deleteById(@PathVariable Long loginId){
        logger.info("删除登录日志");
        loginLogService.deleteById(loginId);
        return ResponseBean.success();
    }
    /**
     * 批量删除
     *
     * @param loginIds
     * @return
     */
    @RequiresPermissions({"loginLog:batchDelete"})
    @DeleteMapping("/batchDelete/{loginIds}")
    public ResponseBean batchDelete(@PathVariable String loginIds) {
        logger.info("批量删除日志");
        String[] idList = loginIds.split(",");
        List<Long> list = new ArrayList<>();
        if (idList.length > 0) {
            for (String s : idList) {
                list.add(Long.parseLong(s));
            }
        }
        loginLogService.batchDelete(list);
        return ResponseBean.success();
    }
    /**
     *  备份登录日志excel
     * @param response
     */
    @PostMapping("/excel")
    @RequiresPermissions("loginLog:save")
    public void export(HttpServletResponse response) {
        logger.info("备份登录日志");
        List<loginLog> loginLogs = this.loginLogService.findAll();
        ExcelKit.$Export(loginLog.class, response).downXlsx(loginLogs, false);
    }

}

